<?php
/**
 * 钩子
 * @return mixed|phphooks|null
 */
function getHooks()
{
    $hooks = getGlobals('phpHooks');
    if ($hooks) return $hooks;

    $hooks = new phphooks();
    setGlobals('phpHooks', $hooks);
    return $hooks;
}

/**
 * 获取模型
 * @param $model
 * @return mixed
 */
function model($model)
{
    $key = "model_m_" . md5($model);
    if (!getGlobals($key)) {
        $file = $model;
        if (!empty($file) && stripos($file, '.') === false) {
            $file .= '.php';
        }
        $tempfile = MDir . $file;
        if ($tempfile) {
            include($tempfile);
        }
        $class_name = $model;
        $class_name = str_replace(".php", '', $class_name);
        $class_name_arr = explode("/", $class_name);
        $class_name = '\\' . M . '\\' . join('\\', $class_name_arr);
        setGlobals($key, new $class_name);
    }
    return getGlobals($key);
}


/**
 * 创建类文件
 * @param $fullname
 * @param $namespace
 * @param $classname
 * @param string $funcname
 */
function createClassFile($fullname, $namespace, $classname, $funcname = "")
{
    if (!$funcname) {
        $funcname = "__construct";
    }
    $content = '<?' . 'php ';
    $content .= <<<CLASS
namespace {$namespace};
class {$classname}
{
    public function {$funcname}()
    {
        echo "todo something...";
    }
}
CLASS;

    if (substr(strtolower($fullname), -4) != ".php") {
        $fullname .= '.php';
    }
    file_put_contents($fullname, $content);
}

/**
 * 在类文件中添加方法
 * @param $fullname
 * @param $funcname
 */
function addFunctionToClassFile($fullname, $funcname)
{
//    return;
    if (!$funcname) return;
    $content = file_get_contents($fullname);
    $last = strrpos($content, '}');
    if (strpos($content, ' ' . $funcname . "(") === false) {
        $content = substr($content, 0, $last);
        $bb = <<<MK
    public function {$funcname}()
    {
        echo "todo something...";
    }
}
MK;
        $content = $content . "\n\n" . $bb;
        file_put_contents($fullname, $content);
    }
}

/**
 * 创建一个控制器文件
 * @param $controllerFullName
 */
function createOneController($controllerFullName)
{
    $controllerFullName = trim($controllerFullName, '/');
    $controller_arr = explode('@', $controllerFullName);
    $controllerName = $controllerFullName;
    $controllerFunction = "";
    if (count($controller_arr) > 1) {
        $controllerName = $controller_arr[0];
        $controllerFunction = $controller_arr[1];

        if (strpos($controllerName, '.php') === false) {
            $controllerName = $controllerName . '.php';
        }
    }


    $fullpath = CDir . $controllerName;
    $dir = dirname($fullpath);
    $start = 0;
    if (strrpos($fullpath, '/') !== false) {
        $start = strrpos($fullpath, '/') + 1;
    }
    $filename = substr($fullpath, $start);
    $filename = file_name($filename);
    $className = substr($filename, 0, -4);
    file_mkdirs($dir, 0777);
    if (dirname($controllerFullName) == '.') {
        $namespace = C;
    } else {
        $namespace = C . '\\' . dirname($controllerFullName);
    }

    if (file_exists($fullpath)) {
        //文件存在则写出方法，找到最后一个大括号，去掉之后加入方法
        $filecontent = strtolower(file_get_contents($fullpath));
        if (strpos($filecontent, '<body') !== false ||
            strpos($filecontent, '<html') !== false ||
            strpos($filecontent, '<div') !== false ||
            strpos($filecontent, '<span') !== false ||
            strpos($filecontent, '<li') !== false ||
            strpos($filecontent, '<ul') !== false ||
            strpos($filecontent, '<ol') !== false
        ) {
            //html文件
        } else {
            if (strpos($filecontent, '}') !== false && strpos($filecontent, 'class ') !== false && strpos($filecontent, 'namespace ') !== false) {
                //是类文件
            } else {
                //不是类文件
                if ($filecontent == "" || trim($filecontent) == "<?php") {
                    createClassFile($fullpath, $namespace, $className, $controllerFunction);
                }
            }
            addFunctionToClassFile($fullpath, $controllerFunction);
        }

    } else {
        //文件不存在，则直接创建
        createClassFile($fullpath, $namespace, $className, $controllerFunction);
    }
}

/**
 * 创建多个控制器文件
 * @param array $controllerArray
 */
function createController($controllerArray = array())
{
    if (count($controllerArray) > 0) {
        foreach ($controllerArray as $item) {
            createOneController($item);
        }
    }
}



/**
 * 获取$_SERVER中的数据
 * @param $k
 * @param  mixed $def 如果空则返回此值
 * @return mixed|string
 */
function getServer($k, $def = '')
{
    return isset($_SERVER[$k]) ? $_SERVER[$k] : $def;
}

/**
 * 将数据设置到$_SERVER中
 * @param $k
 * @param $v
 * @return void
 */
function setServer($k, $v)
{
    if ($v == null) {
        if (isset($_SERVER[$k])) {
            unset($_SERVER[$k]);
        }
    } else {
        $_SERVER[$k] = $v;
    }
}

/**
 * 设置到$GLOBALS变量中
 * @param $k
 * @param $v
 * @return void
 */
function setGlobals($k, $v)
{
//    echo $v;
    if ($v == null) {
        if (isset($GLOBALS[$k])) {
            unset($GLOBALS[$k]);
        }
    } else {
        $GLOBALS[$k] = $v;
    }
}

/**
 * 从$GLOBALS中获取
 * @param $k
 * @param mixed $def 如果空则返回此值
 * @return mixed|string
 */
function getGlobals($k, $def = '')
{
    return isset($GLOBALS[$k]) ? $GLOBALS[$k] : $def;
}



//临时全局变量
$GTMP = array();
/**
 * 获取临时值
 * @param $k
 * @param string $default
 * @return string
 */
function get_tmp_val($k, $default = "")
{
    global $GTMP;
    if (isset($GTMP[$k])) {
        return $GTMP[$k];
    } else {
        return $default;
    }
}

/**
 * 设置临时数据
 * @param $k
 * @param string $v
 */
function set_tmp_val($k, $v = "")
{
    global $GTMP;
    $GTMP[$k] = $v;
}
